package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.UmsResourceCategory;
import cn.zcscloud.mall.service.UmsResourceCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
@CrossOrigin
public class UmsResourceCategoryController {

    @Autowired
    private UmsResourceCategoryService umsResourceCategoryService;

    /**
     * 查询所有后台资源分类
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<UmsResourceCategory>> listAll() {
        LambdaQueryWrapper<UmsResourceCategory> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(UmsResourceCategory::getSort);
        List<UmsResourceCategory> list = umsResourceCategoryService.list(lqw);
        return R.success(list);
    }

    /**
     * 添加后台资源分类
     *
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody UmsResourceCategory umsResourceCategory) {
        umsResourceCategoryService.save(umsResourceCategory);
        return R.success(null);
    }

    /**
     * 根据ID删除后台资源分类
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        umsResourceCategoryService.deleteById(id);
        return R.success(null);
    }

    /**
     * 修改后台资源分类
     *
     * @param umsResourceCategory
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> update(@RequestBody UmsResourceCategory umsResourceCategory, @PathVariable("id") Integer id) {
        umsResourceCategory.setId(id.longValue());
        umsResourceCategoryService.updateById(umsResourceCategory);
        return R.success(null);
    }
}
